package top.yuki.data;

import org.bukkit.configuration.ConfigurationSection;
import org.yaml.snakeyaml.Yaml;
import top.yuki.LevelWorld;

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

public class DayDataManager {
    public HashMap<Long, HashMap<String, HashMap>> dayDataMap;
    private LevelWorld lw;
    private HashMap user_data = null;
    private File dataFold;
    private File dataFile;
    private long startTime;
    private ConfigurationSection config;


    public static DayDataManager init(LevelWorld lw) {

        return new DayDataManager(lw);
    }                //简单生成对象的方法

    public DayDataManager(LevelWorld lw) {
        this.lw = lw;

        this.config = lw.getConfig();
        this.startTime = new Date().getTime();
        this.dataFold = lw.getDataFolder();
        this.dataFile = new File(dataFold + File.separator + "dayData.yml");

        if (!dataFile.exists()) {
            try {
                dataFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        this.dayDataMap = loadData();
        dataInit();

    }
    public void dataInit(){
        if (!dayDataMap.containsKey(startTime)) {
            dayDataMap.put(startTime, new HashMap());
        }
    }
    public HashMap loadData() {
        Yaml yml = new Yaml();
        try {
            InputStream reader = new FileInputStream(dataFile);
            HashMap map = yml.loadAs(reader, HashMap.class);
            if (map == null || map.isEmpty()) {
                startTime = getRefreshTime();
                return new HashMap();
            }
            startTime = (long) map.keySet().iterator().next();

            if (isMoreThanOneDay(new Date())) {

                return new HashMap();
            }
            return map;

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }
    public void detectRefresh(){

        if (isMoreThanOneDay(new Date())) {
            startTime = getRefreshTime();
            this.dayDataMap = new HashMap();
            this.dayDataMap.put(startTime, new HashMap());
        }
    }

    public void save() {
        dataFold = lw.getDataFolder();
        File dataFile = new File(dataFold + File.separator + "dayData.yml");
        Yaml yaml = new Yaml();

            try {
                FileWriter writer = new FileWriter(dataFile);
                yaml.dump(dayDataMap, writer);
            } catch (IOException e) {
                e.printStackTrace();
            }

    }

    public void writeData(UUID uuid, String name, int dayGetExp, int killAmount) {
        String id = String.valueOf(uuid);
        HashMap map1 = new HashMap();
        map1.put("name", name);
        map1.put("dayGetExp", dayGetExp);
        map1.put("killAmount", killAmount);


        HashMap map2 = new HashMap();
        map2.put(id, map1);

        dayDataMap.put(Long.valueOf(startTime), map2);
    }

    public void addDayGetExp(String id, int add) {
        int readData = (int) dayDataMap.get(startTime).get(id).get("dayGetExp");
        dayDataMap.get(startTime).get(id).replace("dayGetExp", readData + add);

    }

    public void initPlayerData(String id) {
        HashMap map = new HashMap();
        map.put("dayGetExp", 0);
        map.put("killAmount", 0);
        dayDataMap.get(startTime).put(id, map);

    }

    public int getDayGetExp(String id) {
        int readData = (int) dayDataMap.get(startTime).get(id).get("dayGetExp");
        return readData;
    }
    //获取凌晨四点时间
    public long getRefreshTime(){
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String today = simpleDateFormat.format(date.getTime());
        long daytime1 = 0;
        try {
            daytime1 = simpleDateFormat.parse(today).getTime();
            daytime1 += 60*1000*60*4;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return daytime1;
    }

    public boolean isMoreThanOneDay(Date date1) {
        long nowTime = date1.getTime();
        if ((nowTime - startTime)>=86400){
            return true;
        }
        return false;
    }


}
